﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Iservices;
using Model;
using EFEntity;
using AutoMapper;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Data;

namespace Services
{
    public class salary_standardDao : Isalary_standardDao
    {
        private readonly MyDbContext mydb;
        private readonly IMapper mapper;
        public salary_standardDao(MyDbContext mydb, IMapper mapper)
        {
            this.mydb = mydb;
            this.mapper = mapper;
        }

        public async Task<int> Add(salary_standard b)
        {
            Esalary_standard etest = mapper.Map<Esalary_standard>(b);
            mydb.Esalary2.Add(etest);
            int result = await mydb.SaveChangesAsync();
            return result;
        }

        public async Task<int> Del(short p)
        {
            Esalary_standard st = mydb.Esalary2.Find(p);
            mydb.Esalary2.Remove(st);
            int result = await mydb.SaveChangesAsync();
            return result;
        }

        public async Task<Dictionary<string, object>> FenYeSelectSelect(FenYe fy)
        {
            SqlParameter[] ps =
            {//@pageSize, @Filter, @tableName, @whereText, @orderText, @keyName, @pageIndex, @rows, @pages
                    new SqlParameter() {ParameterName="@pageSize",Value=fy.PageSize},
                    new SqlParameter() {ParameterName="@Filter",Value="*" },
                    new SqlParameter() {ParameterName="@tableName",Value="salary_standard" },
                    new SqlParameter() {ParameterName="@whereText",Value=fy.Where},
                    new SqlParameter() {ParameterName="@orderText",Value="" },
                    new SqlParameter() {ParameterName="@keyName",Value="Id" },
                    new SqlParameter() {ParameterName="@pageIndex",Value=fy.CurrentPage },
                    new SqlParameter() {ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int },
                    new SqlParameter() {ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int }
                };
            List<Esalary_standard> list = await mydb.Esalary2.FromSqlRaw("exec dbo.proc_FenYeB @pageSize, @Filter, @tableName, @whereText, @orderText, @keyName, @pageIndex, @rows output, @pages output", ps).ToListAsync();
            fy.Rows = (int)ps[7].Value;
            fy.Pages = (int)ps[8].Value;
            //把EO-DTO
            List<salary_standard> list2 = mapper.Map<List<salary_standard>>(list);
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["list"] = list2;
            di["rows"] = fy.Rows;
            di["pages"] = fy.Pages;
            return di;
        }

        public async Task<List<salary_standard>> Select()
        {
            List<Esalary_standard> list = new List<Esalary_standard>();
            await Task.Run(() =>
            {
                list = mydb.Esalary2.ToList();
            });
            List<salary_standard> list2 = mapper.Map<List<salary_standard>>(list);
            return list2;
        }

 
        public async Task<int> Update(salary_standard b)
        {
            Esalary_standard etest = mapper.Map<Esalary_standard>(b);
            mydb.Attach(etest);
            mydb.Entry<Esalary_standard>(etest).State = EntityState.Modified;
            return await mydb.SaveChangesAsync();
        }
    }
}
